<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<body>

<script type="text/javascript">
$(function() {
	djt_FS_editIndex = undefined;
	
	var Sobid=<%=request.getAttribute("Sobid")%>;
	//console.log("Sobid"+Sobid);
	
	$('#djt_FS_dg'+Sobid+'').datagrid({
    	url:'../floatSalary/findFSByPage?id='+Sobid+'',
    	columns:[[
    		{field:'',title:'',checkbox:true},
			{field:'floatSalaryId',title:'编号',width:'60',sortable:true},
			{field:'empName',title:'员工名',width:'100'},
			{field:'floatSalaryName',title:'浮动薪资名',width:'100',editor:{type:'text',options:{required:true}},sortable:true},
			{field:'value',title:'浮动薪资值',width:'100',editor:{type:'numberbox',options:{required:true}},sortable:true},
			{field:'beginDate',title:'开始时间',width:'115',editor:{type:'datebox',options:{required:true}},sortable:true},
			{field:'endDate',title:'结束时间',width:'115',editor:{type:'datebox'},sortable:true},
			{field:'mender',title:'修改者',width:'100'},
			{field:'mendDate',title:'修改时间',width:'115'},
			{field:'remark',title:'备注',width:'100',editor:{type:'text'}},
		]],
		fit:true,
 	   	fitColumns:false,
 	   	//隔行变色
 	   	striped:true,
  	  	nowrap:true,
		toolbar:'#FStb'+Sobid+'',
		pagination:true,
		pageSize:5,
		pageList:[5,10,20,40],
		border:false,
		sortName:'floatSalaryId',
		sortOrder:'asc',
		onAfterEdit : function(rowIndex,rowData,changes){
			var updated = $('#djt_FS_dg'+Sobid+'').datagrid('getChanges','updated');
			var url = '';
			if(updated.length > 0){
				url = '../floatSalary/updateFS';
			}
			var djt_FS_rowData = JSON.stringify(rowData);
			$.post(url,{djt_FS_rowData},function(e){
				var info = JSON.parse(e);
				//console.log(info);
				if(info.success){
					$('#djt_FS_dg'+Sobid+'').datagrid('acceptChanges');
					$.messager.show({
						title:'成功',
						msg:info.msg,
					});
				}else{
					$('#djt_FS_dg'+Sobid+'').datagrid('rejectChanges');
					$.messager.alert('错误',info.msg,'error');
				}
			})
			djt_FS_editIndex = undefined;
			$('#djt_FS_dg'+Sobid+'').datagrid('unselectAll');
		},
		onDblClickRow : function(rowIndex,rowData){
			if(djt_FS_editIndex != undefined){
				$('#djt_FS_dg'+Sobid+'').datagrid('endEdit',djt_FS_editIndex);
				djt_FS_editIndex = undefined;
			}
			if(djt_FS_editIndex == undefined){
				$('#djt_FS_dg'+Sobid+'').datagrid('beginEdit',rowIndex);
				djt_FS_editIndex = rowIndex;
			}
		}
	});

	//保存添加
	$('#djt_FS_savebtn').bind('click', function(){
		if($('#djt_FS_win_form').form('validate')){
			var djt_FS_rowData = $('#djt_FS_win_form').serialize();
			$.ajax({
				url : '../floatSalary/insertFS?floatSalarySobId='+Sobid,
				data : djt_FS_rowData,
				cahce : false,
				dataType : 'json',
				success : function(r){
					$("#djt_FS_win").window('close');
					$.messager.show({
						title:'成功',
						msg:'添加社保信息成功！',
					});
				}
			});
		}
    });
	
	//撤销添加
	$('#djt_FS_reloadbtn').bind('click', function(){
		$("#djt_FS_win_form").form('reset');
    });

});

//保存
function saveFS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	$('#djt_FS_dg'+Sobid+'').datagrid('endEdit',djt_FS_editIndex);
}

//删除
function removeFS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	var rows = $('#djt_FS_dg'+Sobid+'').datagrid('getSelections');
	if(rows.length > 0){
		$.messager.confirm('请确认','您确定要删除当前所有选中的项目吗？',function(r){
			if(r){
				var ids = [];
				for(var i = 0; i < rows.length; i++){
					ids.push(rows[i].floatSalaryId)
				}
				var djt_FS_rowData = JSON.stringify(ids);
				$.post('../floatSalary/deleteFS',{djt_FS_rowData},function(e){
					var info = JSON.parse(e);
					if(info.success){
						$('#djt_FS_dg'+Sobid+'').datagrid('load');
						$('#djt_FS_dg'+Sobid+'').datagrid('unselectAll');
						$.messager.show({
							title:'成功',
							msg:info.msg,
						});
					}else{
						$('#djt_FS_dg'+Sobid+'').datagrid('rejectChanges');
						$.messager.alert('错误',info.msg,'error');
					}
				})
				djt_FS_editIndex = undefined;
			}
		})
	}else{
		$.messager.alert('提示','请选择要删除的记录！','error');
	}
}

//编辑
function editFS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	var rows = $('#djt_FS_dg'+Sobid+'').datagrid('getSelections');
	if(rows.length == 1){
		if(djt_FS_editIndex != undefined){
			$('#djt_FS_dg'+Sobid+'').datagrid('endEdit',djt_FS_editIndex);
		}
		if(djt_FS_editIndex == undefined){
			var index = $('#djt_FS_dg'+Sobid+'').datagrid('getRowIndex',rows[0]);
			$('#djt_FS_dg'+Sobid+'').datagrid('beginEdit',index);
			djt_FS_editIndex = index;
			$('#djt_FS_dg'+Sobid+'').datagrid('unselectAll');
		}
	}else{
		$.messager.show({
			title:'提示',
			msg:'一次只能编辑一行！',
		});
	}
}

//撤销编辑
function rejectFS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	djt_FS_editIndex = undefined;
	$('#djt_FS_dg'+Sobid+'').datagrid('rejectChanges');
	$('#djt_FS_dg'+Sobid+'').datagrid('unselectAll');
} 

//开启添加
function addFS(){
	$('#djt_FS_win').window('open');
}

$("#djt_FS_win").window({
	width:330,
    height:370,
    title:'新增员工浮动薪资信息',
    minimizable:false,
    maximizable:false,
    collapsible:false,
    closed:true,
});

$('#djt_FS_from_dep').combobox({
		    url:'../dep/findAllDep',
		    valueField:'depId',
		    textField:'depName',
		    editable:false,
		    onSelect:function(record)                         
		    {
		    	var url ='../socialSecurity/findEmpByDepID?did='+record.depId;
		     	$('#djt_FS_from_emp').combobox('reload',url);
		    }
});

$('#djt_FS_from_emp').combobox({
	valueField:'empId',
    textField:'empName',
    editable:false
});

$('#djt_FS_from_FSName').textbox({
	required:true
});

$('#djt_FS_from_FSvalue').numberbox({
	required:true
});

$('#djt_FS_from_FSbDate').datebox({
    required:true
});

$('#djt_FS_from_FSeDate').datebox({});

$('#djt_FS_from_FSmark').textbox({});

$('#djt_FS_savebtn').linkbutton({
    iconCls: 'icon-save'
});

$('#djt_FS_reloadbtn').linkbutton({
    iconCls: 'icon-reload'
});

</script>

<div class="easyui-layout" fit="true" border="false">
	<div region="center" border="false">
		<table id="djt_FS_dg${Sobid}"></table>
	
		<div id="FStb${Sobid}">
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addFS()">添加</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeFS()">删除</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editFS()">编辑</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="saveFS()">保存</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="rejectFS()">撤销编辑</a>
		</div>
		
		<!-- 添加的窗口 -->
	<div id="djt_FS_win">	
		<form id="djt_FS_win_form" class="easyui-form" method="post">
			<table>
				<tr>
					<td><label for="depName">部门:</label></td>
					<td><input id="djt_FS_from_dep" name="depName"/></td>
				</tr>
				<tr>
					<td><label for="empName">员工:</label></td>
					<td><input id="djt_FS_from_emp" name="empName" required="required"/></td>
				</tr>
				<tr>
					<td><label for="floatSalaryName">浮动工资名:</label></td>
					<td><input id="djt_FS_from_FSName" name="floatSalaryName"/></td>
				</tr>
				<tr>
					<td><label for="value">浮动工资值:</label></td>
				<td><input id="djt_FS_from_FSvalue"  name="value"/></td>
				</tr>
				<tr>
					<td><label for="beginDate">开始时间:</label></td>
					<td><input id="djt_FS_from_FSbDate" name="beginDate"/></td>
				</tr>
				<tr>
					<td><label for="endDate">结束时间:</label></td>
					<td><input id="djt_FS_from_FSeDate" name="endDate"/></td>
				</tr>
				<tr>
					<td><label for="remark">备注:</label></td>
					<td><input id="djt_FS_from_FSmark" name="remark" type="text" class="easyui-textbox"/></td>
				</tr>
				<tr  style="height:20px"></tr>
				<tr>
					<td><a id="djt_FS_savebtn" href="#" >保存</a></td>
					<td><a id="djt_FS_reloadbtn" href="#" >重置</a></td>
				</tr>
			</table>
		</form> 
	</div>
	</div>	
</div>
</body>